package elion.com.theElion.util.asynectask;

import android.app.Activity;
import android.os.AsyncTask;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import elion.com.theElion.util.HttpUtils;
import elion.com.theElion.util.UIHelper;

import static elion.com.theElion.util.ClassAttrsName.getAttrsName;

/**
 * Created by xiaon on 2017/6/8.
 */

public class ServiceAsy extends AsyncTask<String, Void, String> {
    private List<Map<String, String>> mList;

    private callBack callBack;
    private Object object;
    private Activity content;
    private String name[];


    public ServiceAsy(Object object, Activity content) {
        this.mList = null;
        this.object = object;
        object.getClass();
        name = getAttrsName(object);
        this.content = content;
    }

    @Override
    protected String doInBackground(String... params) {
        System.out.println(params[0]);
        String result = HttpUtils.sendPostMethod(params[0], "utf-8");
        System.out.println("&&&&&&&&&&&&&&&&&&&&");
        System.out.println(result);
        System.out.println("&&&&&&&&&&&&&&&&&&&&");
        return result;
    }


    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result == null && result.equals("")) {
            Toast.makeText(content, "连接超时！！", Toast.LENGTH_SHORT).show();
            return;
        }
        mList = getJson(result);
        if (mList != null) {
            callBack.setListview(mList, name);

        }

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        new UIHelper().showDialogForLoading(content, "正在加载...", true);

    }

    public List<Map<String, String>> getJson(String result) {
        List<Map<String, String>> list = this.mList;

        if (list == null) {
            list = new ArrayList<Map<String, String>>();
            System.out.println("list是空的");
        }
        if (result != null && !result.equals("")) {
            try {
                Map<String, String> map;
                //result = Html.fromHtml(result).toString();
                JSONArray jsonArray = new JSONArray(result);
                JSONObject jsonObject;
                for (int i = 0; i < jsonArray.length(); i++) {

                    map = new HashMap<String, String>();

                    jsonObject = jsonArray.getJSONObject(i);
                    for (int j = 0; j < name.length; j++) {

                        map.put(name[j], jsonObject.getString(name[j]));
                    }
                    list.add(map);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        System.out.println(list.size());

        return list;
    }

    /**
     * @param callBack 设置回调接口，使异步中的方法可以再应用类中进行设置
     */
    public void setCallBack(callBack callBack) {
        this.callBack = callBack;
    }

    public interface callBack {
        void setListview(List<Map<String, String>> list, String name[]);
    }
}
